home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Demo / dns / dnslib.py < prev    next >
Encoding:
Text File  |  1994-10-08  |  14.9 KB  |  101 lines  |  [TEXT/R*ch]

  1. # Domain Name Server (DNS) interface
  2. #
  3. # See RFC 1035:
  4. # ------------------------------------------------------------------------
  5. # Network Working Group                                     P. Mockapetris
  6. # Request for Comments: 1035                                           ISI
  7. #                                                            November 1987
  8. # Obsoletes: RFCs 882, 883, 973
  9. #             DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION
  10. # ----------------------------------------------------------dnslib.py',
  11.         print '[-T] [-r] [-s server] [-t] [-u]',
  12.         print '[qtype [qname]]'
  13.         print '-T:        run testpacker() and exit'
  14.         print '-r:        recursion desired (default not)'
  15.         print '-s server: use server (default %s)' % server
  16.         print '-t:        use TCP protocol'
  17.         print '-u:        use UDP protocol (default)'
  18.         print 'qtype:     query type (default %s)' % \
  19.               dnstype.typestr(qtype)
  20.         print 'qname:     query name (default %s)' % qname
  21.         print 'Recognized qtype values:'
  22.         qtypes = dnstype.typemap.keys()
  23.         qtypes.sort()
  24.         n = 0
  25.         for qtype in qtypes:
  26.             n = n+1
  27.             if n >= 8: n = 1; print
  28.             print '%s = %d' % (dnstype.typemap[qtype], qtype),
  29.         print
  30.         sys.exit(2)
  31.     for o, a in opts:
  32.         if o == '-T': testpacker(); return
  33.         if o == '-t': protocol = 'tcp'
  34.         if o == '-u': protocol = 'udp'
  35.         if o == '-s': server = a
  36.         if o == '-r': rd = 1
  37.     if args[0:]:
  38.         try:
  39.             qtype = eval(string.upper(args[0]), dnstype.__dict__)
  40.         except (NameError, SyntaxError):
  41.             print 'bad query type:', `args[0]`
  42.             sys.exit(2)
  43.     if args[1:]:
  44.         qname = args[1]
  45.     if qtype == dnstype.AXFR:
  46.         print 'Query type AXFR, protocol forced to TCP'
  47.         protocol = 'tcp'
  48.     print 'QTYPE %d(%s)' % (qtype, dnstype.typestr(qtype))
  49.     m = Mpacker()
  50.     m.addHeader(0,
  51.           0, opcode, 0, 0, rd, 0, 0, 0,
  52.           1, 0, 0, 0)
  53.     m.addQuestion(qname, qtype, dnsclass.IN)
  54.     request = m.getbuf()
  55.     if protocol == 'udp':
  56.         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  57.         s.connect((server, port))
  58.         s.send(request)
  59.         reply = s.recv(1024)
  60.     else:
  61.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  62.         s.connect((server, port))
  63.         s.send(pack16bit(len(request)) + request)
  64.         s.shutdown(1)
  65.         f = s.makefile('r')
  66.         header = f.read(2)
  67.         if len(header) < 2:
  68.             print '*** EOF ***'
  69.             return
  70.         count = unpack16bit(header)
  71.         reply = f.read(count)
  72.         if len(reply) != count:
  73.             print '*** Incomplete reply ***'
  74.             return
  75.     u = Munpacker(reply)
  76.     dumpM(u)
  77.     if protocol == 'tcp' and qtype == dnstype.AXFR:
  78.         while 1:
  79.             header = f.read(2)
  80.             if len(header) < 2:
  81.                 print '========== EOF =========='
  82.                 break
  83.             count = unpack16bit(header)
  84.             if not count:
  85.                 print '========== ZERO COUNT =========='
  86.                 break
  87.             print '========== NEXT =========='
  88.             reply = f.read(count)
  89.             if len(reply) != count:
  90.                 print '*** Incomplete reply ***'
  91.                 break
  92.             u = Munpacker(reply)
  93.             dumpM(u)
  94.  
  95.  
  96. # Run test program when called as a script
  97.  
  98. if __name__ == '__main__':
  99.     test()
  100.